package LavEnNyTypeWriter_Stream;

import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

public class WriterFork extends Writer{
	private Writer writerA, writerB; 

	public WriterFork(Writer writerA, Writer writerB) {
		this.writerA = writerA;
		this.writerB = writerB;
	}
	
	@Override
	public void close() throws IOException {
		writerA.close();
		writerB.close();
	}

	@Override 
	public void flush() throws IOException {
		writerA.flush();
		writerB.flush();
	}

	@Override
	public void write(char[] arg0, int arg1, int arg2) throws IOException {
		writerA.write(arg0, arg1, arg2);
		writerB.write(arg0, arg1, arg2);
	}
	
	public static void main(String[] args) throws IOException {
		StringWriter stringWriter = new StringWriter();
		FileWriter fileWriter = new FileWriter("output.txt");
		
		WriterFork forkWriter = new WriterFork(fileWriter, stringWriter);
		
		forkWriter.write("Oh hai!");
		forkWriter.flush();
		forkWriter.close();
		
		System.out.println(stringWriter.toString());
	}
}